﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using DataAccess.DataObject;

namespace EnmineKaitlin.admin
{
    public partial class survey_export : Common.PageBase
    {
        const string module = "manage-poll";
        protected void Page_Load(object sender, EventArgs e)
        {
            if(CheckPermission(module))
            {
            if (!IsPostBack)
            {
                lblFunction.Text = "Báo cáo kết quả khảo sát nhu cầu BĐS Hà Nội";
                grvSurvey.DataSource = _BSurvey.Get(1, 50);
                grvSurvey.DataBind();
            }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            try
            {
                string FromDate = txtFromDate.Text;
                string ToDate = txtToDate.Text;
                if (txtFromDate.Text == "")
                {
                    FromDate = "2012/01/01 12:00";
                }
                if (txtToDate.Text == "")
                {
                    ToDate = "9999/12/12 12:00";
                }
                grvSurvey.DataSource = _BSurvey.Get(DateTime.Parse(FromDate, culture, DateTimeStyles.NoCurrentDateDefault), DateTime.Parse(ToDate, culture, DateTimeStyles.NoCurrentDateDefault), 1, 50);
                grvSurvey.DataBind();
            }
            catch (Exception ex)
            { 
                
            }
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Charset = "utf-8";

            Response.Buffer = true;

            //This will make the browser interpret the output as an Excel file
            Response.AddHeader("Content-Disposition", "filename=KhaoSatNhuCauBDSHaNoi-" + DateTime.Now.ToString("dd-MM-yyyy_hh_mm_ss") + ".xls");
            Response.ContentType = "application/vnd.ms-excel";

            //Prepares the html and write it into a StringWriter
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
            
            FrontDecorator(ref htmlWriter);

            htmlWriter.Write("<h2>Khảo sát nhu cầu BĐS tại Hà Nội</h2>");
            htmlWriter.Write("<table border='1'>");
            htmlWriter.Write("<tr bgcolor='#CCFFCC'><th rowspan='2'>STT</th><th rowspan='2'>Tên KH</th><th rowspan='2'>Độ tuổi</th><th rowspan='2'>Nghề nghiệp</th><th rowspan='2'>Địa chỉ</th><th colspan='4'>Loại BĐS</th><th rowspan='2'>Diện tích</th><th rowspan='2'>Khoảng giá</th><th colspan='8'>Hướng</th><th rowspan='2'>KH biết PT qua đâu</th><th rowspan='2'>KV KH quan tâm</th><th rowspan='2'>Lý do chọn KV</th></tr>");
            htmlWriter.Write("<tr bgcolor='#CCFFCC'><th>Liền kề</th><th>Biệt thự</th><th>Chung cư</th><th>Khác</th><th>Đông</th><th>Tây</th><th>Nam</th><th>Bắc</th><th>ĐN</th><th>ĐB</th><th>TN</th><th>TB</th>");
            string FromDate = txtFromDate.Text;
            string ToDate = txtToDate.Text;
            if (txtFromDate.Text == "")
            {
                FromDate = "2012/01/01 12:00";
            }
            if (txtToDate.Text == "")
            {
                ToDate = "9999/12/12 12:00";
            }
            IList<OSurvey> lstSurvey = _BSurvey.Get(DateTime.Parse(FromDate, culture, DateTimeStyles.NoCurrentDateDefault), DateTime.Parse(ToDate, culture, DateTimeStyles.NoCurrentDateDefault), 1, 100);
            for (int i = 0; i < lstSurvey.Count; i++)
            {
                if ((i + 1) % 2 == 0)
                {
                    htmlWriter.Write("<tr align='center' bgcolor='#CCFFFF'>");
                }
                else {
                    htmlWriter.Write("<tr align='center'>");
                }
                htmlWriter.Write("<td>"+(i+1)+"</td>");
                htmlWriter.Write("<td>"+lstSurvey[i].GuestName+"</td>");
                htmlWriter.Write("<td>"+lstSurvey[i].Age+"</td>");
                htmlWriter.Write("<td>"+lstSurvey[i].Job+"</td>");
                htmlWriter.Write("<td>"+lstSurvey[i].Address+"</td>");

                switch (lstSurvey[i].EstateType)
                { 
                    case "Liền kề":
                        htmlWriter.Write("<td>X</td><td></td><td></td><td></td>"); 
                        break;
                    case "Biệt thự":
                        htmlWriter.Write("<td></td><td>X</td><td></td><td></td>");
                        break;
                    case "Cung cư":
                        htmlWriter.Write("<td></td><td></td><td>X</td><td></td>");
                        break;
                    case "Khác":
                        htmlWriter.Write("<td></td><td></td><td></td><td>X</td>");
                        break;
                }                

                htmlWriter.Write("<td>"+lstSurvey[i].Area+"</td>");
                htmlWriter.Write("<td>" + lstSurvey[i].Price + "</td>");

                switch (lstSurvey[i].Direction)
                { 
                    case "Đông":
                        htmlWriter.Write("<td>X</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>"); 
                        break;
                    case "Tây":
                        htmlWriter.Write("<td></td><td>X</td><td></td><td></td><td></td><td></td><td></td><td></td>");
                        break;
                    case "Nam":
                        htmlWriter.Write("<td></td><td></td><td>X</td><td></td><td></td><td></td><td></td><td></td>");
                        break;
                    case "Bắc":
                        htmlWriter.Write("<td></td><td></td><td></td><td>X</td><td></td><td></td><td></td><td></td>");
                        break;
                    case "ĐN":
                        htmlWriter.Write("<td></td><td></td><td></td><td></td><td>X</td><td></td><td></td><td></td>");
                        break;
                    case "ĐB":
                        htmlWriter.Write("<td></td><td></td><td></td><td></td><td></td><td>X</td><td></td><td></td>");
                        break;
                    case "TN":
                        htmlWriter.Write("<td></td><td></td><td></td><td></td><td></td><td></td><td>X</td><td></td>");
                        break;
                    case "TB":
                        htmlWriter.Write("<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>X</td>");
                        break;

                }

                htmlWriter.Write("<td>" + lstSurvey[i].FutaLand + "</td>");
                htmlWriter.Write("<td>" + lstSurvey[i].Region + "</td>");
                htmlWriter.Write("<td>" + lstSurvey[i].Why + "</td>");
                htmlWriter.Write("</tr>");
            }
            htmlWriter.Write("</table>");

            RearDecorator(ref htmlWriter);
                        
            Response.Write(stringWriter.ToString());
            Response.End();
            //Microsoft.Office.Interop.Excel.Application xlApp=null;
            //Excel.Workbook wb=null;
            //Excel.Worksheet ws=null;
            //Object oMissing = System.Reflection.Missing.Value;
            //Object oTrue = true;
            //Object oFalse = false;
            //string filename = "";
            //try
            //{                
            //    xlApp = new Microsoft.Office.Interop.Excel.Application();

            //    if (xlApp == null)
            //    {
            //        Response.Write("EXCEL could not be started. Check that your office installation and project references are correct.");
            //        return;
            //    }
            //    xlApp.Visible = true;
            //    string template = Server.MapPath(urlexceltemplate);
            //    Object oTemplate = template;
            //    wb = xlApp.Workbooks.Add(oTemplate);
            //    if (wb == null)
            //    {
            //        Response.Write("EXCEL could not be started. Check that your office installation and project references are correct.");
            //        return;
            //    }
            //    ws = (Excel.Worksheet)wb.Worksheets[1];

            //    if (ws == null)
            //    {
            //        Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
            //    }

            //    // Select the Excel cells, in the range c1 to c7 in the worksheet.
            //    Excel.Range cells  = ws.Cells;

            //    if (cells == null)
            //    {
            //        Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
            //    }
            //    string FromDate = txtFromDate.Text;
            //    string ToDate = txtToDate.Text;
            //    if (txtFromDate.Text == "")
            //    {
            //        FromDate = "2012/01/01 12:00";
            //    }
            //    if (txtToDate.Text == "")
            //    {
            //        ToDate = "9999/12/12 12:00";
            //    }
            //    IList<OSurvey> lstSurvey = _BSurvey.Get(DateTime.Parse(FromDate, culture, DateTimeStyles.NoCurrentDateDefault), DateTime.Parse(ToDate, culture, DateTimeStyles.NoCurrentDateDefault), 1, 100);
            //    Object RowIndex = "4";
            //    Object ColumnIndex = "";
            //    for (int i = 0; i < lstSurvey.Count;i++ )
            //    {
            //        ColumnIndex = "A";
            //        cells[RowIndex, ColumnIndex] = i+1;
            //        ColumnIndex = "B";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].GuestName;
            //        ColumnIndex = "C";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].Age;
            //        ColumnIndex = "D";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].Job;
            //        ColumnIndex = "E";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].Address;
            //        switch (lstSurvey[i].EstateType)
            //        { 
            //            case "Liền kề":
            //                ColumnIndex = "F";                            
            //                break;
            //            case "Biệt thự":
            //                ColumnIndex = "G";
            //                break;
            //            case "Trung Cư":
            //                ColumnIndex = "H";
            //                break;
            //            case "Khác":
            //                ColumnIndex = "I";
            //                break;
            //        }
            //        cells[RowIndex, ColumnIndex] = "X";
            //        ColumnIndex = "J";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].Area;
            //        ColumnIndex = "K";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].Price;
            //        switch (lstSurvey[i].Direction)
            //        { 
            //            case "Đông":
            //                ColumnIndex = "L";
            //                break;
            //            case "Tây":
            //                ColumnIndex = "M";
            //                break;
            //            case "Nam":
            //                ColumnIndex = "N";
            //                break;
            //            case "Bắc":
            //                ColumnIndex = "O";
            //                break;
            //            case "ĐN":
            //                ColumnIndex = "P";
            //                break;
            //            case "ĐB":
            //                ColumnIndex = "Q";
            //                break;
            //            case "TN":
            //                ColumnIndex = "R";
            //                break;
            //            case "TB":
            //                ColumnIndex = "S";
            //                break;
            //        }                    
            //        cells[RowIndex, ColumnIndex] = "X";
            //        ColumnIndex = "T";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].EnmineKaitlin;
            //        ColumnIndex = "U";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].Region;
            //        ColumnIndex = "V";
            //        cells[RowIndex, ColumnIndex] = lstSurvey[i].Why;

            //        int rowindex = int.Parse(RowIndex.ToString());
            //        rowindex += 1;
            //        RowIndex = "" + rowindex + "";
            //    }                                
            //    string date = DateTime.Now.ToString("dd-MM-yyyy_hh_mm_ss");
            //    date = date.Replace("/", "-");
            //    filename = Server.MapPath("~/Uploads/KhaoSatNhuCauBDSHaNoi-"+date+".xlsx");
            //    Object oFileName = filename;
            //    wb.SaveCopyAs(oFileName);                
            //    wb.Close(oFalse,oMissing,oMissing);
            //    xlApp.Quit();
            //}
            //catch (Exception ex)
            //{
            //    wb.Close(oFalse, oMissing, oMissing);
            //    xlApp.Quit();
            //}
            //Download(filename);
        }
        private void FrontDecorator(ref HtmlTextWriter writer)
        {
            writer.WriteFullBeginTag("HTML");
            writer.Write("xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n");
            writer.Write("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n");
            writer.WriteFullBeginTag("Head");
            writer.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\n");
            writer.Write("<meta name=ProgId content=Excel.Sheet>\n");
            writer.Write("<meta name=Generator content=\"Microsoft Excel 12\">\n");
            writer.RenderBeginTag(HtmlTextWriterTag.Style);
            writer.Write("<!--");
            writer.Write("body\n");
            writer.Write("{font-family: Arial;font-size: 10px;}");
            writer.Write(".style1\n");
            writer.Write("{color: #000000; padding-left: 3px;}");
            writer.Write(".style2\n");
            writer.Write("{color: #000000; font-weight: normal;}");
            writer.Write("-->");
            writer.RenderEndTag();
            writer.WriteEndTag("Head");
            writer.WriteFullBeginTag("Body");
        }
        private void RearDecorator(ref HtmlTextWriter writer)
        {
            writer.WriteEndTag("Body");
            writer.WriteEndTag("HTML");
        }
    }
}
